package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by selbylei on 2015/6/26.
 */
@Controller
@RequestMapping("v20/reply")
public class ReplyAction extends BaseAction {


    /**
     * 添加评论
     * @param myId
     * @param eventCommentsId
     * @param toId
     * @param content
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "eventCommentsId") long eventCommentsId,
            @RequestParam(value = "toId",required = false) Integer toId,
            @RequestParam(value = "content") String content,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            EventComments eventComments = eventCommentsDao.getCommentsListById(eventCommentsId);

            Date now = new Date();

            Reply reply = new Reply();
            reply.setMyId(myId);
            AppUser my = userDao.getUserById(myId);
            reply.setMyName(my.getName());
            reply.setMyHeadImage(my.getHeadImage());
            reply.setEventId(eventComments.getEventId());
            reply.setEventCommentsId(eventCommentsId);
            if (toId!=null){
                reply.setType("回复");
                reply.setToId(toId);
                reply.setToName(userDao.getUserById(toId).getName());
            }else {
                reply.setType("评论");
                reply.setToId(eventComments.getAppUserId());
                reply.setToName(userDao.getUserById(eventComments.getAppUserId()).getName());
            }
            reply.setContent(content);
            reply.setCreateTime(now);
            reply.setUpdateTime(now);

            replyDao.addReply(reply);


            Notification notification = new Notification();
            notification.setEventsId(eventComments.getEventId());
            notification.setMyId(myId);
            notification.setMyName(my.getName());
            notification.setMyImageHead(my.getHeadImage());
            AppUser toUser = userDao.getUserById(eventComments.getAppUserId());
            notification.setToId(toUser.getId());
            notification.setToName(toUser.getName());
            List<Image> imageList = imageDao.getImageByEventCommentsId(eventCommentsId);
            notification.setToImage(imageList.get(0).getPath());
            notification.setContent(content);
            notification.setType(2);
            notification.setTarget(2);
            notification.setCreateTime(now);
            notification.setUpdateTime(now);
            notificationDao.addNotification(notification);


            MessageStatus messageStatus = new MessageStatus();
            messageStatus.setAppUserId(eventComments.getAppUserId());
            messageStatus.setType(2);
            messageStatus.setMessageId(notification.getId());
            messageStatusDao.addMessageStatus(messageStatus);


            resultMap.put("reply",reply);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


}
